package sync.hw.demo01;

/**
 * @author lwj
 * @date 2025-03-12
 * @vesion 1.0.0
 */
public class MyRunnable implements Runnable{
    /**
     * 银行信息
     */
    private BankInfo bankInfo;
    /**
     * 用户信息
     */
    private UserInfo userInfo;
    private String lock = "";

    // 构造方法
    public MyRunnable() {}
    public MyRunnable(BankInfo bankInfo,UserInfo userInfo) {
        this.bankInfo = bankInfo;
        this.userInfo = userInfo;
    }

    @Override
    public void run() {
        synchronized (lock){
            double money = userInfo.getMoney();
            double temp = bankInfo.getMoney() - money;
            if(temp < 0){
                System.out.println("余额不足！");
            }else {
                bankInfo.setMoney(temp);
                System.out.println(userInfo.getName() +"使用" + userInfo.getBankName() + "卡,取出的钱为:" + money + "元，还剩：" + bankInfo.getMoney() + "元！");
            }
        }
    }
}
